<?
if(!$CFG) require_once('config.php');


if(key_exists('debug',$_GET) && $_GET['debug']) $debug = true;
else $debug = false;

if(key_exists('type',$_GET)) $type = $_GET['type'];
else $type = false;

header('Content-Type: application/x-javascript; charset=utf8');

//if($debug) {
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
	header('Cache-Control: no-cache, must-revalidate');
	header('Pragma: no-cache');
/*}
else {
	$atime = fileatime($CFG->GownFull_javascript_file);
	header('Last-Modified: '.gmdate('D, d M Y H:i:s',$atime).' GMT');
	header('Expires: '.gmdate('D, d M Y H:i:s',$atime + 86400).' GMT');
}
*/
if($debug) readfile($CFG->GownFull_javascript_debug_file);
else readfile($CFG->GownFull_javascript_file);

// check language

if(key_exists('lang',$_GET)) {
	$path = $CFG->GownFull_LANGPath.$_GET['lang'].'.js';
	if(file_exists($path)) {
		readfile($path);
	}
	else { // this language does not exists
		printf("\n//Language \"%s\" does not exist.\n",$_GET['lang']);
	}
}

// change the config
printf("GownFullConfig.gownfull_base_url = '%s';\r\n",$CFG->GownFull_URL);
printf("GownFullConfig.css_file = '%s';\r\n",$CFG->GownFull_URL . 'gownfull.css');
printf("GownFullConfig.getim_url = '%s';\r\n",$CFG->GownFull_URL . 'getim.php');
printf("GownFullConfig.help_url = '%s';\r\n",$CFG->GownFull_URL . 'help/');

if($type == 'frame') {
	printf("new GownFull(new FramesetGownFullBuilder);\r\n");

	if($debug) printf("GownFull.instance.modifiers.push(new UnicodeImageOutputModifier(GownFull.instance));\r\n");
	
	if(!$_GET['noregdownload']) {
		foreach($CFG->Available_IM as $im) {
			if(key_exists('url',$im)) printf("GownFull.instance.RegisterDownload('%s',\"%s\",\"%s\");\r\n",$im['objname'],$im['displayname'],$im['url']);
			else printf("GownFull.instance.RegisterDownload('%s',\"%s\");\r\n",$im['objname'],$im['displayname']);
		}
	}
}
else if($type == 'nocreate') {
}
else {
	// Test if frameset
	printf("if(FramesetInstaller.instance.IsFrameset()) {\r\n");
	printf("  FramesetInstaller.code_url = '%s';\r\n",$CFG->GownFull_BASE . $_SERVER["REQUEST_URI"] . '&type=frame');
	printf("  FramesetInstaller.instance.InstallGownFull(FramesetInstaller.instance.SelectLargestAreaFrame());\r\n");
	printf("}\r\n");
	printf("else {\r\n");
	printf("new GownFull(new GenericGownFullBuilder);\r\n");

	// install input logger
	printf("GownFull.instance.AttachInputLogger(new RawInputLogger);\r\n");
	
	if(!$_GET['noregdownload']) {
		foreach($CFG->Available_IM as $im) {
			if(key_exists('url',$im)) printf("GownFull.instance.RegisterDownload('%s',\"%s\",\"%s\");\r\n",$im['objname'],$im['displayname'],$im['url']);
			else printf("GownFull.instance.RegisterDownload('%s',\"%s\");\r\n",$im['objname'],$im['displayname']);
		}
	}
	printf("}\r\n");
}
?>
